package org.emet.service;

import org.emet.dao.CarriageDao;
import org.emet.dao.RegistrationDao;
import org.emet.dao.RegistrationResourceDao;
import org.emet.dao.VehicleDao;
import org.emet.entity.biz.Carriage;
import org.emet.entity.biz.Registration;
import org.emet.entity.biz.RegistrationResource;
import org.emet.entity.biz.Vehicle;
import org.emet.jee.BusinessException;
import org.emet.jee.core.dao.Page;
import org.emet.service.dto.RegistrationCriteria;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RemotingDestination
public class RegistrationService {
	@Autowired
	private RegistrationDao registrationDao;
	@Autowired
	private RegistrationResourceDao resourceDao;
	@Autowired
	private VehicleDao vehicleDao;
	@Autowired
	private CarriageDao carriageDao;

	private static Logger logger = LoggerFactory.getLogger(RegistrationService.class);

	public RegistrationResource getDeadProvenFile(Long regId) {
		return resourceDao.findUniqueBy("regId", regId);
	}

	public void save(Registration entity, byte[] deadProvenFile) {
		Transaction tx = registrationDao.getSession().beginTransaction();
		try {
			Vehicle vehicle = vehicleDao.get(entity.getVehicleId());
			Carriage c = carriageDao.findLastestByVeh(vehicle.getId());
			// c.setReturnDate(entity.getArriveDateTime());
			VehicleStatusUpdateHelper.change2IdleStatus(vehicle);
			registrationDao.save(entity);
			if (deadProvenFile != null && deadProvenFile.length > 0) {
				RegistrationResource rr = getDeadProvenFile(entity.getId());
				if (rr == null) {
					rr = new RegistrationResource();
					rr.setRegId(entity.getId());
				}
				rr.setDeadProvenPic(deadProvenFile);
				resourceDao.save(rr);
			}
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public Registration get(Long id) {
		try {
			return registrationDao.get(id);
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public Registration getByBizNo(String no) {
		try {
			return registrationDao.findUniqueBy("bizNo", no);
		} catch (ObjectNotFoundException e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}

	public void del(Long... ids) {
		for (Long id : ids) {
			if (notExist(id)) {
				continue;
			}
			try {
				registrationDao.delete(id);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				throw new BusinessException("发生异常!");
			}
		}
	}

	private boolean notExist(Long id) {
		return get(id) == null;
	}

	public Page<Registration> search(Page<Registration> page, RegistrationCriteria criteria) {
		try {
			return registrationDao.search(page, criteria);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BusinessException("发生异常!");
		}
	}
}
